<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="经销商ID" prop="dealerId">
        <el-input
          v-model="queryParams.dealerId"
          placeholder="请输入经销商ID"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="经销商名称" prop="dealerName">
        <el-input
          v-model="queryParams.dealerName"
          placeholder="请输入经销商名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="预约单号" prop="reservationNo">
        <el-input
          v-model="queryParams.reservationNo"
          placeholder="请输入预约单号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="工单编号" prop="roNo">
        <el-input
          v-model="queryParams.roNo"
          placeholder="请输入工单编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择状态" clearable>
          <el-option
            v-for="dict in dict.type.repair_order_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="品牌名称" prop="brandName">
        <el-select v-model="queryParams.brandName" placeholder="请选择品牌名称" clearable>
          <el-option
            v-for="dict in dict.type.car_brand"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="工单类型" prop="workOrderType">
        <el-select v-model="queryParams.workOrderType" placeholder="请选择工单类型" clearable>
          <el-option
            v-for="dict in dict.type.repair_order_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="维修工人" prop="maintainWorker">
        <el-select v-model="queryParams.maintainWorker" placeholder="请选择维修工人" clearable>
          <el-option
            v-for="dict in dict.type.repair_order_worker"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="进厂时间" prop="incomingTime">
        <el-date-picker clearable
          v-model="queryParams.incomingTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择进厂时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="是否洗车" prop="isWash">
        <el-select v-model="queryParams.isWash" placeholder="请选择是否洗车" clearable>
          <el-option
            v-for="dict in dict.type.no_and_yes"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="是否保养" prop="isMaintain">
        <el-select v-model="queryParams.isMaintain" placeholder="请选择是否保养" clearable>
          <el-option
            v-for="dict in dict.type.no_and_yes"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="工时单价" prop="labourPrice">
        <el-input
          v-model="queryParams.labourPrice"
          placeholder="请输入工时单价"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="总金额" prop="totalAmount">
        <el-input
          v-model="queryParams.totalAmount"
          placeholder="请输入总金额"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="理赔单号" prop="insurantionNo">
        <el-input
          v-model="queryParams.insurantionNo"
          placeholder="请输入理赔单号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="保险公司" prop="insurantionName">
        <el-select v-model="queryParams.insurantionName" placeholder="请选择保险公司" clearable>
          <el-option
            v-for="dict in dict.type.repair_insurantion_company"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="审核人名称" prop="auditorName">
        <el-input
          v-model="queryParams.auditorName"
          placeholder="请输入审核人名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="审核时间" prop="auditorTime">
        <el-date-picker clearable
          v-model="queryParams.auditorTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择审核时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['aftersale:repairOrder:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['aftersale:repairOrder:edit']"
        >查阅详情</el-button>
      </el-col>
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="danger"-->
<!--          plain-->
<!--          icon="el-icon-delete"-->
<!--          size="mini"-->
<!--          :disabled="multiple"-->
<!--          @click="handleDelete"-->
<!--          v-hasPermi="['aftersale:repairOrder:remove']"-->
<!--        >删除</el-button>-->
<!--      </el-col>-->
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['aftersale:repairOrder:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="repairOrderList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="工单ID" align="center" prop="id" />
      <el-table-column label="经销商ID" align="center" prop="dealerId" />
      <el-table-column label="经销商名称" align="center" prop="dealerName" />
      <el-table-column label="预约单号" align="center" prop="reservationNo" />
      <el-table-column label="工单编号" align="center" prop="roNo" />
      <el-table-column label="状态" align="center" prop="status">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.repair_order_status" :value="scope.row.status"/>
        </template>
      </el-table-column>
      <el-table-column label="品牌名称" align="center" prop="brandName">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.car_brand" :value="scope.row.brandName"/>
        </template>
      </el-table-column>
      <el-table-column label="工单类型" align="center" prop="workOrderType">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.repair_order_type" :value="scope.row.workOrderType"/>
        </template>
      </el-table-column>
      <el-table-column label="维修工人" align="center" prop="maintainWorker">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.repair_order_worker" :value="scope.row.maintainWorker"/>
        </template>
      </el-table-column>
      <el-table-column label="进厂时间" align="center" prop="incomingTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.incomingTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="进厂里程" align="center" prop="inMileage" />
      <el-table-column label="进厂油位" align="center" prop="incomingOilLevel" />
      <el-table-column label="是否洗车" align="center" prop="isWash">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.no_and_yes" :value="scope.row.isWash"/>
        </template>
      </el-table-column>
      <el-table-column label="是否保养" align="center" prop="isMaintain">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.no_and_yes" :value="scope.row.isMaintain"/>
        </template>
      </el-table-column>
      <el-table-column label="工时单价" align="center" prop="labourPrice" />
      <el-table-column label="总金额" align="center" prop="totalAmount" />
      <el-table-column label="理赔单号" align="center" prop="insurantionNo" />
      <el-table-column label="保险公司" align="center" prop="insurantionName">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.repair_insurantion_company" :value="scope.row.insurantionName"/>
        </template>
      </el-table-column>
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="审核人id" align="center" prop="auditorId" />
      <el-table-column label="审核人名称" align="center" prop="auditorName" />
      <el-table-column label="审核时间" align="center" prop="auditorTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.auditorTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['aftersale:repairOrder:edit']"
          >查阅详情</el-button>
<!--          <el-button-->
<!--            size="mini"-->
<!--            type="text"-->
<!--            icon="el-icon-delete"-->
<!--            @click="handleDelete(scope.row)"-->
<!--            v-hasPermi="['aftersale:repairOrder:remove']"-->
<!--          >删除</el-button>-->
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改维修工单对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
<!--        <el-form-item label="预约单号" prop="reservationNo">-->
<!--          <el-input v-model="form.reservationNo" placeholder="请输入预约单号" />-->
<!--        </el-form-item>-->
        <el-form-item label="工单编号" prop="roNo">
          <el-input v-model="form.roNo" placeholder="请输入工单编号(若不输入，后台自动生成)" />
        </el-form-item>
        <el-form-item label="状态" prop="status">
          <el-select v-model="form.status" placeholder="请选择状态">
            <el-option
              v-for="dict in dict.type.repair_order_status"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="品牌名称" prop="brandName">
          <el-select v-model="form.brandName" placeholder="请选择品牌名称">
            <el-option
              v-for="dict in dict.type.car_brand"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="工单类型" prop="workOrderType">
          <el-select v-model="form.workOrderType" placeholder="请选择工单类型">
            <el-option
              v-for="dict in dict.type.repair_order_type"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="维修工人" prop="maintainWorker">
          <el-select v-model="form.maintainWorker" placeholder="请选择维修工人" :disabled="true">
            <el-option
              v-for="dict in dict.type.repair_order_worker"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="进厂时间" prop="incomingTime">
          <el-date-picker clearable
            v-model="form.incomingTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择进厂时间"
            :disabled="true"
          ></el-date-picker>
        </el-form-item>
        <el-form-item label="进厂里程" prop="inMileage">
          <el-input v-model="form.inMileage" placeholder="请输入进厂里程" :disabled="true"/>
        </el-form-item>
        <el-form-item label="进厂油位" prop="incomingOilLevel">
          <el-input v-model="form.incomingOilLevel" placeholder="请输入进厂油位" :disabled="true"/>
        </el-form-item>
        <el-form-item label="是否洗车" prop="isWash">
          <el-select v-model="form.isWash" placeholder="请选择是否洗车">
            <el-option
              v-for="dict in dict.type.no_and_yes"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="是否保养" prop="isMaintain">
          <el-select v-model="form.isMaintain" placeholder="请选择是否保养">
            <el-option
              v-for="dict in dict.type.no_and_yes"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="工时单价" prop="labourPrice">
          <el-input v-model="form.labourPrice" placeholder="请输入工时单价" :disabled="true"/>
        </el-form-item>
        <el-form-item label="总金额" prop="totalAmount">
          <el-input v-model="form.totalAmount" placeholder="请输入总金额" :disabled="true"/>
        </el-form-item>
        <el-form-item label="理赔单号" prop="insurantionNo">
          <el-input v-model="form.insurantionNo" placeholder="请输入理赔单号" />
        </el-form-item>
        <el-form-item label="保险公司" prop="insurantionName">
          <el-select v-model="form.insurantionName" placeholder="请选择保险公司">
            <el-option
              v-for="dict in dict.type.repair_insurantion_company"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
        </el-form-item>

        <el-divider content-position="center">已申请的预约单</el-divider>
        <!-- 查询维修预约单 -->
        <el-table v-loading="loading" :data="repairReservationList" @selection-change="handleSelectionChange01">
          <el-table-column v-if="title=='添加维修工单'" type="selection" width="55" align="center"/>
          <el-table-column label="id" align="center" prop="id" />
          <el-table-column label="预约单号" align="center" prop="reservationNo" />
          <el-table-column label="预约状态" align="center" prop="states">
            <template slot-scope="scope">
              <dict-tag :options="dict.type.repair_reservation_status" :value="scope.row.states"/>
            </template>
          </el-table-column>
          <el-table-column label="经销商id" align="center" prop="dealerId" />
          <el-table-column label="经销商名称" align="center" prop="dealerName" />
          <el-table-column label="车牌号" align="center" prop="license" />
          <el-table-column label="服务项目" align="center" prop="serviceType">
            <template slot-scope="scope">
              <dict-tag :options="dict.type.repair_service_type" :value="scope.row.serviceType"/>
            </template>
          </el-table-column>
          <el-table-column label="姓名" align="center" prop="username" />
          <el-table-column label="电话" align="center" prop="phone" />
          <el-table-column label="预约时间" align="center" prop="reservationDate" width="180">
            <template slot-scope="scope">
              <span>{{ parseTime(scope.row.reservationDate, '{y}-{m}-{d}') }}</span>
            </template>
          </el-table-column>
          <el-table-column label="vin" align="center" prop="vin" />
          <el-table-column label="是否二手车" align="center" prop="isUsedCar">
            <template slot-scope="scope">
              <dict-tag :options="dict.type.no_and_yes" :value="scope.row.isUsedCar"/>
            </template>
          </el-table-column>
          <el-table-column label="备注" align="center" prop="remark" />
          <el-table-column label="审核人名称" align="center" prop="auditorName" />
          <el-table-column label="审核时间" align="center" prop="auditorTime" width="180">
            <template slot-scope="scope">
              <span>{{ parseTime(scope.row.auditorTime, '{y}-{m}-{d}') }}</span>
            </template>
          </el-table-column>
        </el-table>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm" v-if="title=='添加维修工单'">确 定</el-button>
        <el-button @click="cancel" >取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listRepairOrder, getRepairOrder, delRepairOrder, addRepairOrder, updateRepairOrder } from "@/api/aftersale/repairOrder";
import {listRepairReservation} from "@/api/aftersale/repairReservation";

export default {
  name: "RepairOrder",
  dicts: ['car_brand', 'no_and_yes', 'repair_order_status', 'repair_order_worker', 'repair_insurantion_company', 'repair_order_type'],
  data() {
    return {
      //根据工单id查询出来的预约单号
      reservationNoTemp:null,
      //已预约的维修单
      repairReservationList:[],
      //选中的预约单号
      reservationNos:[],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 维修工单表格数据
      repairOrderList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        dealerId: null,
        dealerName: null,
        reservationNo: null,
        roNo: null,
        status: null,
        brandName: null,
        workOrderType: null,
        maintainWorker: null,
        incomingTime: null,
        isWash: null,
        isMaintain: null,
        labourPrice: null,
        totalAmount: null,
        insurantionNo: null,
        insurantionName: null,
        auditorName: null,
        auditorTime: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        // roNo: [
        //   { required: true, message: "工单编号不能为空", trigger: "blur" }
        // ],
        status: [
          { required: true, message: "状态不能为空", trigger: "change" }
        ],
        workOrderType: [
          { required: true, message: "工单类型不能为空", trigger: "change" }
        ],
        brandName: [
          { required: true, message: "品牌名称不能为空", trigger: "change" }
        ],
        isWash: [
          { required: true, message: "是否洗车不能为空", trigger: "change" }
        ],
        isMaintain: [
          { required: true, message: "是否保养不能为空", trigger: "change" }
        ],
        insurantionNo: [
          { required: true, message: "理赔单号不能为空", trigger: "change" }
        ],
        insurantionName: [
          { required: true, message: "保险公司不能为空", trigger: "change" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询维修工单列表 */
    getList() {
      this.loading = true;
      listRepairOrder(this.queryParams).then(response => {
        this.repairOrderList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.queryParams= {
        pageNum: 1,
          pageSize: 10,
          dealerId: null,
          dealerName: null,
          reservationNo: null,
          roNo: null,
          status: null,
          brandName: null,
          workOrderType: null,
          maintainWorker: null,
          incomingTime: null,
          isWash: null,
          isMaintain: null,
          labourPrice: null,
          totalAmount: null,
          insurantionNo: null,
          insurantionName: null,
          auditorName: null,
          auditorTime: null,
      },
      this.form = {
        id: null,
        dealerId: null,
        dealerName: null,
        reservationNo: null,
        roNo: null,
        status: null,
        license: null,
        vin: null,
        brandName: null,
        workOrderType: null,
        maintainWorker: null,
        incomingTime: null,
        inMileage: null,
        incomingOilLevel: null,
        isWash: null,
        isMaintain: null,
        labourPrice: null,
        totalAmount: null,
        insurantionNo: null,
        insurantionName: null,
        remark: null,
        auditorId: null,
        auditorName: null,
        auditorTime: null,
        createTime: null,
        updateTime: null,
        createBy: null,
        updateBy: null,
        createUserName: null,
        updateUserName: null,
        version: null,
        deleteFlag: null
      };
      reservationNoTemp:null;
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    // 实销上报单多选框选中数据
    handleSelectionChange01(selection) {
      this.reservationNos = selection.map(item => item.reservationNo)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加维修工单";
      //查询预约单
      this.getrepairReservationList();
    },
    getrepairReservationList() {
      this.loading = true;
      this.queryParams.states=2;
      listRepairReservation(this.queryParams).then(response => {
        this.repairReservationList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getRepairOrder(id).then(response => {
        this.form = response.data;
        this.reservationNoTemp = response.data.reservationNo;
        this.open = true;
        this.title = "维修工单详情";
        //根据预约单号，查询预约单号详情信息
        this.getrepairReservationListById(this.reservationNoTemp);
      });
    },
    getrepairReservationListById(reservationNoTemp){
      this.loading = true;
      this.queryParams.reservationNo = reservationNoTemp;
      listRepairReservation(this.queryParams).then(response => {
        this.repairReservationList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateRepairOrder(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            if (this.reservationNos.length != 1){
              this.$message.error('单次必须且只能选择一个预约单号！');
              return;
            }
            this.form.reservationNo = this.reservationNos[0];
            addRepairOrder(this.form).then(response => {
              this.$modal.msgSuccess("维修工单成功生成！");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除维修工单编号为"' + ids + '"的数据项？').then(function() {
        return delRepairOrder(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('aftersale/repairOrder/export', {
        ...this.queryParams
      }, `repairOrder_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
